/**
 * 
 */
package com.ekingstar.framework.util;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import ognl.Ognl;

import org.hibernate.transform.ResultTransformer;

/**
 * 
 *
 */
public final class HibernateTransformerUtil {

	
	public static class AliasToBeanResultTransformer implements ResultTransformer{
		private final Class resultClass;
		private final Map maps = new LinkedHashMap();
		public AliasToBeanResultTransformer(Class resultClass) {
			if ( resultClass == null ) {
				throw new IllegalArgumentException( "resultClass cannot be null" );
			}
			this.resultClass = resultClass;
		}

		public List transformList(List collection) {
			// TODO Auto-generated method stub
			return collection;
		}

		public Object transformTuple(Object[] tuple, String[] aliases) {
			// TODO Auto-generated method stub
			Object result = null;
			try{
			 result = resultClass.newInstance();
			for (int i = 0 ; i < aliases.length;i++){
				try{
				Ognl.setValue(aliases[i].replaceAll("_", "."), result, tuple[i]);
				}catch(Throwable e){
					e.printStackTrace();
				}
			}
			}catch(Throwable e){
				e.printStackTrace();
			}
			return result;
		}
		
	};
	public static ResultTransformer aliasToBeanTransformer(Class targetClass){
		return new AliasToBeanResultTransformer(targetClass);
	}
}
